﻿#include "../QPTypes.h"
#include "FuncTools.h"
#include "qlabel.h"
#include "qmimedatabase.h"
#include "qmimetype.h"
#include "qnamespace.h"
#include "qscreen.h"
#include "qsizepolicy.h"

#include <QDebug>
#include <QIcon>
#include <QStyle>
#include <QApplication>
#include <string>
#include <QSizePolicy>
#ifdef _WIN32
#include <tchar.h>
#include <Windows.h>
#include <Netlistmgr.h>
#else
#include <QFile>
#include <netdb.h>
#include <arpa/inet.h>
#endif

void FunTools::init()
{

}

QIcon FunTools::StdIcon(int num)
{
    QStyle* style = QApplication::style();
    QIcon icon = style->standardIcon((QStyle::StandardPixmap)num);
//    QPixmap m_pic = icon.pixmap(icon.actualSize(QSize(LIco->width()-9,LIco->height()-5)));//size自行调整
    return icon;
}

bool FunTools::isNetOnline()
{
    //检测操作系统是否能连通外部链接
#ifdef _WIN32
    bool bol = false; //是否联网,联网 true,不联网 false
    CoInitialize(NULL);
    //  通过NLA接口获取网络状态
    IUnknown *pUnknown = NULL;
    HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown);
    if (SUCCEEDED(Result))
    {
        INetworkListManager *pNetworkListManager = NULL;
        if (pUnknown)
            Result = pUnknown->QueryInterface(IID_INetworkListManager, (void **)&pNetworkListManager);
        if (SUCCEEDED(Result))
        {
            VARIANT_BOOL IsConnect = VARIANT_FALSE;
            if (pNetworkListManager)
                Result = pNetworkListManager->get_IsConnectedToInternet(&IsConnect);
            if (SUCCEEDED(Result))
            {
                bol = (IsConnect == VARIANT_TRUE) ? true : false;
            }
        }
        if (pNetworkListManager)
            pNetworkListManager->Release();
    }
    if (pUnknown)
        pUnknown->Release();
    CoUninitialize();
    return bol;
#else
    bool flag = false;
    char ip[100] = {0};
    QVector<const char *> t_Hostname;
    t_Hostname.push_back("www.baidu.com");
    t_Hostname.push_back("www.163.com");
    t_Hostname.push_back("www.qq.com");
    for (int i = 0;i < t_Hostname.size();++i)
    {
        struct hostent *he;
        struct in_addr **addr_list;
        if ((he = gethostbyname(t_Hostname[i])) == nullptr)
        {
            flag = false;
        }
        else
        {
            addr_list = (struct in_addr **)he->h_addr_list;
            for (i = 0; addr_list[i] != nullptr; i++)
            {
                strcpy(ip, inet_ntoa(*addr_list[i]));
                flag = true;
                break;
            }
            break;
        }
    }
    return flag;
#endif
}

QString FunTools::WeekE2C(const QString &English)
{
    QString week;
    if(English == QString("Monday"))
    {
        return "星期一";
    }
    if(English == QString("Tuesday"))
    {
        return "星期二";
    }
    if(English == QString("Wednesday"))
    {
        return "星期三";
    }
    if(English == QString("Thursday"))
    {
        return "星期四";
    }
    if(English == QString("Friday"))
    {
        return "星期五";
    }
    if(English == QString("Saturday"))
    {
        return "星期六";
    }
    if(English == QString("Sunday"))
    {
        return "星期日";
    }
    return English;
}

void FunTools::SetPosition(QWidget *parents, QP::POSITION position)
{
    switch (position) {
        
    case QP::POSITION::LeftTOP:
        ToLeft(parents,Qt::AlignTop);
        break;
    case QP::POSITION::LeftCenter:
        ToLeft(parents);
        break;
    case QP::POSITION::LeftBottom:
        ToLeft(parents,Qt::AlignBottom);
        break;
    case QP::POSITION::HVCenter:
        ToCenter(parents);
        break;
    case QP::POSITION::RightTOP:
        ToRight(parents,Qt::AlignLeft);
        break;
    case QP::POSITION::RightCenter:
        ToRight(parents);
        break;
    case QP::POSITION::RightBottom:
        ToRight(parents,Qt::AlignBottom);
        break; 
    default:
        break;
    }
    return;
}

void FunTools::ToCenter(QWidget *parents)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    QRect RectObj = parents->screen()->geometry();
    int win_wid = RectObj.width() - parents->width();
    int win_hig = RectObj.height() - parents->height();
    parents->move( win_wid  / 2, win_hig / 2.5);

#else
    parents->move (
        (QApplication::desktop()->width() - parents->width())/2,
        (QApplication::desktop()->height() - parents->height())/2
        );
#endif
    
}

void FunTools::ToRight(QWidget *parents,Qt::Alignment alignment)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    parents->setGeometry(QStyle::alignedRect(
        Qt::RightToLeft,
        alignment,
        parents->size(),
        parents->screen()->availableGeometry()));
#else
    parents->move (
        (QApplication::desktop()->width() - parents->width()),
        (QApplication::desktop()->height() - parents->height())/2
        );
#endif
}
void FunTools::ToLeft(QWidget *parents, Qt::Alignment alignment)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
    parents->setGeometry(QStyle::alignedRect(
        Qt::LeftToRight,
        alignment,
        parents->size(),
        parents->screen()->availableGeometry())
                         );
#else
    parents->move (
        (QApplication::desktop()->width() - parents->width()),
        (QApplication::desktop()->height()/2 - parents->height())
        );
#endif
}

QWidget *FunTools::AddVLine(const QString& HexColor, int w)
{
    QLabel *line = new QLabel();
    line->setFixedWidth(w);
    line->setStyleSheet(QString("background-color:%1;").arg(HexColor));
    
    QSizePolicy sizePolicy2=line->sizePolicy();
    sizePolicy2.setHorizontalPolicy(QSizePolicy::Expanding);
    line->setSizePolicy(sizePolicy2);
    
    return line;
}

QWidget *FunTools::AddHLine(const QString& HexColor,int h)
{
    QLabel *line = new QLabel();
    line->setFixedHeight(h);
    line->setStyleSheet(QString("background-color:%1;").arg(HexColor));
    
    QSizePolicy sizePolicy2=line->sizePolicy();
    sizePolicy2.setHorizontalPolicy(QSizePolicy::Expanding);
    line->setSizePolicy(sizePolicy2);
    
    return line;
}

///
/// \brief FunTools::setStyle   可设置整个程序的样式表也可设置单个窗口的样式表
/// \param style                样式表文件路径
/// \param parent               指定要设置样式表的窗口，默认参数为：nullptr
///
void FunTools::setStyle(const QString &style,QWidget *parent)
{
    
    QFile qssFile(style);
    qssFile.open(QFile::ReadOnly);
    if(qssFile.isOpen())
    {
        QString qss = QString(qssFile.readAll());
        if(parent == nullptr)
        {
            qApp->setStyleSheet(qss);
        }
        else
        {
            parent->setStyleSheet(qss);
        }
        qssFile.close();
        return;
    }
    qout << "qss样式表文件加载失败";
}

/**
 * \brief FunTools::setSizePolicy 设置窗口大小策略
 * \param wdiget
 * \param hor：enum Policy {
                    Fixed = 0,
                    Minimum = GrowFlag,
                    Maximum = ShrinkFlag,
                    Preferred = GrowFlag | ShrinkFlag,
                    MinimumExpanding = GrowFlag | ExpandFlag,
                    Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
                    Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
               }; 
**/
void FunTools::setSizePolicy(QWidget *wdiget,QSizePolicy::Policy hor)
{
    QSizePolicy sizePolicy1=wdiget->sizePolicy();
    sizePolicy1.setHorizontalPolicy(hor);
    wdiget->setSizePolicy(sizePolicy1);
}

bool FunTools::getAVType(const QString &AVFile)
{
    QMimeDatabase MindDB;
    QMimeType MinTp = MindDB.mimeTypeForFile(AVFile);
    if(MinTp.name().startsWith("audio/"))
    {
        return true;
    }
    else if(MinTp.name().startsWith("video/"))
    {
        return true;
    }
    return false;
}
